<?php

/**
 * SvcPHP
 *
 * Copyright (C) 2012 João Francisco Biondo Trinca
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package    SvcPHP
 * @author     João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @copyright  2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://code.google.com/p/svcphp/
 *
 */

/**
 * Time plugin class
 */
class Svc_Template_Plugin_Time implements Svc_Template_Plugin
{
    /**
     * Creates a new instance of the url plugin.
     *
     */
    public function __construct()
    {
    }

    /**
     * Returns the plugin id.
     *
     * @return string
     */
    public function id()
    {
        return 'time';
    }

    /**
     * Installs the translator into the template engine.
     *
     * @param Svc_TemplateData $data
     */
    public function register(&$data)
    {
        // $data->register('timeago', 'Svc_Template_Plugin_Time_Function');
        $data->register('timeago', function ($time)
        {
            return Svc_Template_Plugin_Time::timeago($time);
        });
    }

    /**
     * put your comment there...
     *
     * @param mixed $time
     */
    public static function timeago($time)
    {
        if (is_string($time))
        {
            $time = strtotime($time);
        }

        return Svc_Template_TimePlugin::humanize($time);
    }

    /**
     * Humianize the time.
     *
     * @param mixed $timestamp
     *
     * @return string
     */
    private static function humanize($timestamp)
    {
        $chunks = array(array(60 * 60 * 24 * 365, 'year'), array(60 * 60 * 24 * 30, 'month'), array(60 * 60 * 24 * 7, 'week'), array(60 * 60 * 24, 'day'), array(60 * 60, 'hour'), array(60, 'minute'), array(1, 'second'),);

        $today = time();
        $since = $today - $timestamp;

        for ($i = 0, $j = count($chunks); $i < $j; $i++)
        {
            $seconds = $chunks[$i][0];
            $name = $chunks[$i][1];

            if (($count = floor($since / $seconds)) != 0)
            {
                break;
            }
        }

        $print = 'time.' . (($count == 1) ? $name : $name . 's');

        return Svc_Application::instance()->getTranslator()->translate($print, $count);
    }
}